package com.mind.core.net.codec;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

import org.apache.log4j.Logger;

import com.mind.core.net.msg.Message;

public class GameEncoder extends MessageToByteEncoder {
	private static final Logger logger = Logger.getLogger(GameEncoder.class);
	@Override
	protected void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out)
			throws Exception {
		if (!(msg instanceof Message)) {
			throw new Exception(
					"This encoder only encode mop's IMessage");
		}
		Message message=(Message)msg;
		ByteBuf buf =ctx.alloc().buffer(message.getInitBufferLength());
		message.setBuffer(buf);
		if(!message.write()){
			throw new Exception("encoder  wrong!message code="+message.getMessageType());
		}
		out.writeBytes(buf);
	}
}
